/**
 * @author Pawel Gadecki <pidzama@gmail.com>
 * 
 */
package tools;

import java.io.*;
import javax.swing.*;
import javax.swing.table.*;

public class ExcelExporter {

    public static void exportTable(JTable table, File file) throws IOException {
        try {
            TableModel model = table.getModel();
            FileWriter out = new FileWriter(file);


            for (int i = 0; i < model.getColumnCount(); i++) {
                String nagl = new String(model.getColumnName(i) + "\t");
                out.write(new String(nagl.getBytes("Cp1250")));
            }
            out.write("\n");
            for (int i = 0; i < model.getRowCount(); i++) {
                for (int j = 0; j < model.getColumnCount(); j++) {

                    String str;
                    if (j > 0) {
                        str = new String(model.getValueAt(i, j).toString().replace('.', ',') + "\t");
                    } else {
                        str = new String(model.getValueAt(i, j).toString() + "\t");
                    }
                    out.write(new String(str.getBytes("Cp1250")));
                }
                out.write("\n");
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
